#####################################################################
# File:         Message.py
# Product:      KnowledgeEngine
#
# Copyright:    Copyright (c) 2002, Faulkner Ltd.
# Author:       Brian Oliver
#
# Description:  This file defines the Message class to
#               encapsulate the concept of a system Message.
#
# See Also:     Messaging, MessageListener
#####################################################################

#####################################################################
## KnowledgeEngine Library Imports
# (none)

#####################################################################
## Zope Library Imports
# (none)
 
#####################################################################
## Python Library Imports
# (none)

#####################################################################
## Message Class Definition
class Message:
    """
        A class to represent the concept of a system Message.
    """
        
    #---------------------------------------------------------------#
    
    #the message topic
    topicName = ""
    
    #the message payload
    payload = {}

    #has the message been consumed (to stop further broadcasting)
    isConsumed = 0
    
    #---------------------------------------------------------------#

    def __init__(self, topicName, **payload):
        """
            Constructor for the Message
        """
        
        self.topicName = topicName
        self.isConsumed = 0

        self.payload = {}
        for key in payload.keys():
            self.payload[key] = payload[key]
            
    #---------------------------------------------------------------#

    def getTopicName(self):
        """
            Return the name of the topic for the message
        """
        return self.topicName
        
    #---------------------------------------------------------------#

    def setConsumed(self):
        """
            Set that the message has been consumed and no longer
            requires processing
        """
        self.isConsumed = 1       

    #---------------------------------------------------------------#

    def hasBeenConsumed(self):
        """ 
            Return if the message has been consumed and no longer
            requires processing
        """
        return self.isConsumed
       
    #---------------------------------------------------------------#

    def __len__(self):
        """
            Return the size of the message payload
        """
        return len(self.payload)

    #---------------------------------------------------------------#

    def __getitem__(self, key):
        """
            Return the specified item in the payload, None otherwise
        """
        if key in self.payload:
            return self.payload[key]
        else:
            return None

    #---------------------------------------------------------------#

    def __delitem__(self, key):
        """
            Remove the specified item from the payload
        """
        del self.payload[key]
            
    #---------------------------------------------------------------#

    def __setitem__(self, key, value):
        """
            Set the specified item in the payload
        """
        self.payload[key] = value

    #---------------------------------------------------------------#
       
    def __contains__(self, key):
        """
            Returns if the specified item is in the payload
        """
        return key in self.payload
        
    #---------------------------------------------------------------#

    def getPayload(self):
        """
            Return the payload keys
        """
        return self.payload.keys()

    #---------------------------------------------------------------#

#####################################################################
## Class Initialisation
# (none)
